/**
 * Created with IntelliJ IDEA.
 * Description:
 * 给一个链表的头节点 head ，判断链表中是否有环
 * User: xwl
 */

class ListNode8 {
    int val;
    ListNode8 next;
    ListNode8(int x) {
         val = x;
         next = null;
    }
}

public class Test10 {
    public boolean hasCycle(ListNode8 head) {
        // 定义俩个指针，分别是快慢指针
        ListNode8 fast = head;
        ListNode8 slow = head;
        // 实现
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            // 相遇即是环
            if (slow == fast) {
                return true;
            }
        }
        // 返回
        return false;
    }
}
